<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/layui/css/layui.css">
    <link rel="stylesheet" type="text/css" href="/css/bootstrap-select.min.css">
    <script src="/js/libs/jquery-2.1.1.min.js"></script>
    <script src="/js/bootstrap/bootstrap.min.js"></script>
    <script src="/js/bootstrap/bootstrap-select.min.js"></script>
    <script src="/js/jq.js"></script>
    <script src="/js/common.js"></script>
    <script src="/layui/layui.js"></script>
    <script src="/js/dict.js"></script>
    <script src="/js/libs/jquery.ztree.all-3.5.min.js"></script>
    <script src="/js/my/ztree-menu.js"></script>
    <script src="/js/my/permission.js"></script>
    <script src="/js/plugin/datatables/jquery.dataTables.min.js"></script>
    <script src="/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
</head>
<body>
<div>
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <div>
            <header style="height: 100%">
                <div align="left">
                    <table style="width: 100%">
                        <tr>
                            <td>
                                <form id="form" class="form-inline" method="post" action="/borrow/export" enctype="multipart/form-data">
                                    <div class="form-group">
                                        编码：<input id="name" name="name" type="text" class="form-control" placeholder="编号">
                                        商品编码：<input id="goodsCode" name="goodsCode" type="text" class="form-control" placeholder="商品编码">
                                        商品名称：<input id="goodsName" name="goodsName" type="text" class="form-control" placeholder="商品名称">
                                        归还时间:<input type="text" class="form-control" placeholder="开始时间" id="beginActualReturnTime">
                                                <input type="text" class="form-control" placeholder="结束时间" id="endActualReturnTime">
                                    </div>
                                </form>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <button id="searchBt" class="layui-btn layui-btn-sm search"><i class="layui-icon">&#xe615;</i>搜索</button>
                            </td>
                            <td align="right">
                                <!--<iframe id="export" style="display:none"></iframe>
                                <button class='layui-btn layui-btn-sm' permission="fas:borrow:export" onclick="exportData()">
                                    <i class='layui-icon'>&#xe601;</i> 导出
                                </button>-->
                                <button class="layui-btn layui-btn-sm" permission="fas:borrow:add" onclick="openIFrame('/pages/fas/borrow/addBorrow.html','添加借用单')">
                                    <i class="layui-icon">&#xe608;</i> 添加
                                </button>
                            </td>
                        </tr>
                    </table>
                </div>
            </header>
            <div>
                <div class="widget-body no-padding">
                    <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                        <thead>
                        <tr>
                            <th>编码</th>
                            <th>借用商品</th>
                            <th>商品编码</th>
                            <th>单位</th>
                            <th>规格</th>
                            <th>数量</th>
                            <th>借用方</th>
                            <th>借用方部门</th>
                            <th>出借方</th>
                            <th>出借方部门</th>
                            <th>预计归还时间</th>
                            <th>归还时间</th>
                            <th>状态</th>
                            <th>创建人</th>
                            <th>创建时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

<script type="text/javascript">
    layui.use(['layer','laydate'], function(){
        var layer = layui.layer;
        var layDate = layui.laydate;
        var upload = layui.upload;
        layDate.render({
            elem: '#beginActualReturnTime'
        });
        layDate.render({
            elem: '#endActualReturnTime'
        });
    });


    var pers = checkPermission();

    var example;
    function init(){
        example =
            $('#dt-table').DataTable({
                "displayLength" : 10,
                "scrollY": "calc(100vh - 150px)",
                "searching": false,
                "processing": true,
                "serverSide": true,
                "language": {
                    "url": "/js/plugin/datatables/Chinese.lang"
                },
                "ajax": {
                    "url": "/borrow/list",
                    "type": "get",
                    "data": function (d) {
                        d.name = $('#name').val().trim();
                        d.goodsCode = $('#goodsCode').val().trim();
                        d.goodsName = $('#goodsName').val().trim();
                        d.beginActualReturnTime = $('#beginActualReturnTime').val().trim();
                        d.endActualReturnTime = $('#endActualReturnTime').val().trim();
                    }
                },
                "dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-4 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-8' p v>>",
                "columns": [
                    {"data": "name", "defaultContent": ""},
                    {"data": "goodsName", "defaultContent": ""},
                    {"data": "goodsCode", "defaultContent": ""},
                    {"data": "goodsUnit", "defaultContent": ""},
                    {"data": "specification", "defaultContent": ""},
                    {"data": "borrowerNum", "defaultContent": ""},
                    {"data": "borrowerPersonnelName", "defaultContent": ""},
                    {"data": "borrowerDepartmentName", "defaultContent": ""},
                    {"data": "lenderPersonnelName", "defaultContent": ""},
                    {"data": "lenderDepartmentName", "defaultContent": ""},
                    {
                        "data": "estimateReturnTime",
                        "defaultContent": "",
                        "render": function (data, type, row) {
                            var diffDays = DateDiff(data, new Date().Format());
                            if (row.flowState == 2) {
                                if (diffDays > 1) {
                                    return "<span style='color: green;'>" + data + "</span>";
                                } else if (diffDays === 1) {
                                    return "<span style='color: orange;'>" + data + "</span>";
                                } else {
                                    return "<span style='color: red;'>" + data + "</span>";
                                }
                            } else {
                                return data;
                            }

                        }
                    },
                    {"data": "actualReturnTime", "defaultContent": ""},
                    {
                        "data": "flowState",
                        "defaultContent": "",
                        "render": function (data) {
                            return getDict('borrowFlowState')[data];
                        }
                    },
                    {"data": "createName", "defaultContent": ""},
                    {"data": "createTime", "defaultContent": ""},
                    {
                        "data": "",
                        "defaultContent": "",
                        "orderable": false,
                        "render": function (data, type, row) {
                            var id = row['id'];
                            var flowState=row['flowState'];
                            var href = "/pages/fas/borrow/updateBorrow.html?id=" + id;
                            var edit = '';
                            var submission = '';    //提交
                            var adopt = '';         //通过
                            var revert = '';        //归还
                            var withdraw = '';        //撤回
                            if(flowState == 0) {//待提交状态
                                edit = buttonEdit(href, "fas:borrow:edit", pers);
                                submission = buttonOther(id, "1", "提交", "fas:borrow:submission", pers);
                            }else if(flowState == 1) {//已提交状态
                                withdraw = buttonOther(id, "0", "撤回", "fas:borrow:withdraw", pers);
                                adopt = buttonOther(id, "2", "通过", "fas:borrow:adopt", pers);
                            }else if(flowState == 2){//待归还状态
                                revert = buttonOther(id, "3", "归还", "fas:borrow:revert", pers);
                            }
                            var del = buttonDel(id, "fas:borrow:del", pers);
                            return edit + submission + adopt + revert + withdraw + del;
                        }
                    },

                ],
                "createdRow": function ( row, data, index ) {
                    $(row).bind({
                        click: function () {
                            if ($(this).hasClass('success') ) {
                                $(this).removeClass('success');
                            } else {
                                example.$('tr.success').removeClass('success');
                                $(this).addClass('success');
                            }
                        },
                        dblclick: function () {
                            if ($.inArray("fas:borrow:edit", pers) >= 0) {
                                var id = data.id;
                                openIFrame("/pages/fas/borrow/updateBorrow.html?id=" + id);
                            }
                        }
                    });
                }
            });
    }

    function del(id){
        layer.confirm('确定要删除吗？', {
            btn : [ '确定', '取消' ]
        }, function() {
            $.ajax({
                type : 'delete',
                url : '/borrow/'+id,
                success : function(data) {
                    example.ajax.reload();
                    layer.msg("删除成功");
                }
            });

            layer.close(1);
        });
    }

    //提交审批
    function approval(id,type,title){
        layer.confirm('确定要'+title+'吗？', {
            btn : [ '确定', '取消' ]
        }, function() {
            var uri="/borrow/approval";
            $.ajax({
                type : 'post',
                url : uri,
                data : {
                    'id':id,
                    'flowState':type
                },
                success : function(data) {
                    example.ajax.reload();
                    layer.msg(data);
                }
            });
            layer.close(1);
        });
    }
    //生成审批按钮的方法
    function buttonOther(id,type,title, permission, pers){
        if(permission != ""){
            if ($.inArray(permission, pers) < 0) {
                return "";
            }
        }
        var btn = $("<button class='layui-btn layui-btn-xs' title='"+title+"' onclick=\"approval('"+id+"','"+type+"','"+title+"')\">"+title+"</button>");

        return btn.prop("outerHTML");
    }

    $("#searchBt").click(function(){
        example.ajax.reload();
    });

    init();

    // 导出
    function exportData() {
        layer.confirm('请选择要导出的方式', {
            btn: ['导出数据','导出空白模板'] //按钮
        }, function(index){
            $('#form').attr('action', '/borrow/export/1').submit();
            layer.close(index);
        }, function(){
            $('#form').attr('action', '/borrow/export/0').submit();
        });
    }
</script>
